<?php

namespace console\controllers;

use Yii;
use yii\console\Controller;
use yii\helpers\Json;
use yii\helpers\StringHelper;

class FooController extends Controller
{
    /**
     * @desc php ./yii foo/zp
     * @return void
     * @Date: 2024/11/6
     */
    public function actionZp()
    {
        ini_set('max_execution_time', '0');
        ini_set('memory_limit', '-1');


    }

    /**
     * @desc php ./yii foo/json
     * @return void
     * @Date: 2024/11/6
     */
    public function actionJson()
    {
        $data1 = [
            'a' => '1',
            'b' => '<p>xxx</p>',
            'c' => "<script>alert('Ha ha ha ~')</script>",
        ];
        $str1 = Json::htmlEncode($data1);
        $str2 = Json::encode($data1);
        echo $str1 . PHP_EOL;
        //{"a":"1","b":"\u003Cp\u003Exxx\u003C\/p\u003E","c":"\u003Cscript\u003Ealert(\u0027Ha ha ha ~\u0027)\u003C\/script\u003E"}

        echo $str2 . PHP_EOL;
        //{"a":"1","b":"<p>xxx</p>","c":"<script>alert('Ha ha ha ~')</script>"}

        $data2 = Json::decode($str1);

        print_r($data2);
    }

    /**
     * @desc php ./yii foo/string
     * @return void
     * @Date: 2024/11/6
     */
    public function actionString()
    {
        $path1 = '/home/default/yii2/frontend/web/uploads/2024/11/06/abc.pdf';
        $basename1 = StringHelper::basename($path1);
        echo $basename1 . PHP_EOL;//abc.pdf

        $path2 = '/home/default/yii2/frontend/web/uploads/2024/11/06/asd.pdf';
        $dirname2 = StringHelper::dirname($path2);
        echo $dirname2 . PHP_EOL;// /home/default/yii2/frontend/web/uploads/2024/11/06

        $str3 = "我爱北京天安门呢<p>123</p><span>456</span><div>789</div><ol>000</ol>";
        $str31 = StringHelper::truncate($str3, 12, '...', null, true);
        echo $str31 . PHP_EOL;//我爱北京天安门呢<p>123</p><span>4</span>...

        $str32 = StringHelper::truncate($str3, 12, '...', null, false);
        echo $str32 . PHP_EOL;// 我爱北京天安门呢<p>1...


    }

}
